// SHA1.java

package app.search.server;

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.lang.StringBuilder;

public class SHA1 {
  
  public static String encode(String string) {
    try {
      MessageDigest sha1 = MessageDigest.getInstance("SHA1");
      byte[] digest = sha1.digest((string).getBytes());
      return bytesToString(digest);
    } catch(Exception e) {
      return "ERROR";
    }
  }
  
  private static String bytesToString(byte[] bytes) {
    StringBuilder string = new StringBuilder();
    for (byte b: bytes) {
      String hexString = Integer.toHexString(0x00FF & b);
      string.append(hexString.length() == 1 ? "0" + hexString : hexString);
    }
    return string.toString();
  }

}

// *EOF*
